package edu.java.texbooks.scjp.collections.test03.comparison.arrays;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import edu.java.texbooks.scjp.collections.test03.comparison.arrays.comparator.StudentComparatorByFacNum;
import edu.java.texbooks.scjp.collections.test03.comparison.arrays.comparator.StudentComparatorByMarks;

public class TestStudent {

	static Student student1 = new Student("Pesho Ivanov", 56214, 5.34);
	static Student student2 = new Student("Asen Ivanov", 56378, 5.12);

	static Student student3 = new Student("Petq Milcheva", 46214, 3.34);
	static Student student4 = new Student("Asq Ivanova", 67378, 4.12);

	static Student[] myStudentArray = { student1, student2, student3, student4 };

	public static void main(String[] args) {
		// test Comparable and Comparator on Objects
		// test01();

		// test Comparable and Comparator on Arrays
		// test02();

		// test Comparable and Comparator on Collections
		test03();
	}

	public static void test01() {
		System.out.println(student1);
		System.out.println(student2);
		int compareResult = 0;

		System.out.println("\n---------- Compare by name: ----------");
		compareResult = student1.compareTo(student2);
		System.out
				.println("student1.compareTo(student2)  --> " + compareResult);

		System.out
				.println("\n---------- Compare by faculty number: ----------");
		StudentComparatorByFacNum scfn = new StudentComparatorByFacNum();
		compareResult = scfn.compare(student1, student2);
		System.out.println("scfn.compare(student1, student2)  --> "
				+ compareResult);

		System.out.println("\n---------- Compare by marks: ----------");
		StudentComparatorByMarks scm = new StudentComparatorByMarks();
		compareResult = scm.compare(student1, student2);
		System.out.println("scm.compare(student1, student2)  --> "
				+ compareResult);
	}

	public static void test02() {
		Student[] studentArray = new Student[4];
		studentArray[0] = student1;
		studentArray[1] = student2;
		studentArray[2] = student3;
		studentArray[3] = student4;

		System.out.println(Arrays.toString(studentArray));

		// sort by name
		Arrays.sort(studentArray);
		System.out.println("---------------------------------------------");
		System.out.println("Sort by name"
				+ "(as it is in the Comparable implementation).");
		System.out.println(Arrays.toString(studentArray));

		// sort by faculty number
		System.out.println("---------------------------------------------");
		StudentComparatorByFacNum scfn = new StudentComparatorByFacNum();
		Arrays.sort(studentArray, scfn);
		System.out.println("Sort by faculty number.");
		System.out.println(Arrays.toString(studentArray));

		// sort by marks
		System.out.println("---------------------------------------------");
		StudentComparatorByMarks scm = new StudentComparatorByMarks();
		Arrays.sort(studentArray, scm);
		System.out.println("Sort by marks.");
		System.out.println(Arrays.toString(studentArray));
	}

	public static void test03() {
		List<Student> studentArrayList = new ArrayList<Student>();

		// method Arrays.asList(studentArray) --> converts from Array to List
		studentArrayList.addAll(Arrays.asList(myStudentArray));

		System.out.println(studentArrayList);

		// sort by name
		System.out.println("---------------------------------------------");
		Collections.sort(studentArrayList);
		System.out.println("Sort by name "
				+ "as it is in Comparable implementation.");
		System.out.println(studentArrayList);

		// sort by faculty number
		System.out.println("---------------------------------------------");
		StudentComparatorByFacNum scfn = new StudentComparatorByFacNum();
		Collections.sort(studentArrayList, scfn);
		System.out.println("Sort by faculty number.");
		System.out.println(studentArrayList);

		// sort by marks
		System.out.println("---------------------------------------------");
		StudentComparatorByMarks scm = new StudentComparatorByMarks();
		Collections.sort(studentArrayList, scm);
		System.out.println("Sort by marks.");
		System.out.println(studentArrayList);

	}
}
